<template>
	<view>
		<view class="u-flex u-relative u-padding-20" style="background-color: #FFFFFF;">
			<view class="list-search u-flex-1" style="padding: 0;"><!-- min-width: 55%; -->
				<u-search placeholder="请输入要搜索的账号或手机号码" shape="square" bg-color="#F7F7F7" v-model="keyword" @change="$u.debounce(searchChange, 300)" animation :show-action="false"></u-search>
			</view>
			<view class="u-flex u-col-center u-row-center" @click="popupShow = !popupShow" style="height: 64rpx;" v-if="userInfo.roleName != '院区管理员'">
				<text class="u-margin-left-10 u-margin-right-10 u-font-14">{{statusOptions[statusCurrent].label}}</text>
				<u-icon :name="popupShow ? 'arrow-up' : 'arrow-down'" color="#666666" size="30"></u-icon>
			</view>
		</view>
		<u-popup v-model="popupShow" mode="top" z-index="100">
			<view class="u-flex u-flex-wrap" style="margin-top: 100rpx;padding: 30rpx 30rpx 10rpx 30rpx;">
				<view class="downMenu" v-for="(item, index) in statusOptions" :key="index" @click="downSelChange(index)" :style="statusCurrent == index && 'background: #ebf6ff;color: #007dfd;border: 1rpx solid #43b1ff;'">
					{{ item.label }}
					<text v-if="item.extra > 0 && item.value != null">
						（{{ item.extra }}个账号）
					</text>
				</view>
			</view>
		</u-popup>
		<block v-if="list.length == 0">
			<view :style="{ height: (systemInfo.windowHeight - 120) + 'px'}">
				<u-empty src="https://gxmanager.oss-cn-shenzhen.aliyuncs.com/xfqczbgl_static/no-data.png" icon-size="300" text="暂无院区数据，赶紧添加一条吧！"></u-empty>
			</view>
		</block>
		<block v-else>
			<scroll-view scroll-y>
				<view class="base-list">
					<view class="base-item" v-for="(item,index) in list" :key="index">
						<view class="silk-ribbon" v-if="item.roleName">
							<span class="silk-ribbon1" :class="item.roleName.indexOf('管理员') > 0 ? 'green' : 'yellow'"><span>{{ item.roleName }}</span></span>
						</view>
						<view class="base-info row">
							<view class="base-content u-flex-col u-row-between">
								<view>
									<view class="notic-title">用户账号：{{ item.userName }}</view>
									<view class="notic-title">手机号码：{{ item.phone }}</view>
									<view class="notic-title">真实姓名：{{ item.nickName }}</view>
									<view class="notic-desc text-gray u-flex">所属院区：{{ item.deptName }}</view>
								</view>
							</view>
						</view>
						<view class="base-handle">
							<u-button class="btn" type="primary" @click="navTo('accountForm', { type: 1, id: item.userId })">编辑</u-button>
							<u-button class="btn" type="error" @click="delAccount(item.userId)">删除</u-button>
						</view>
					</view>
				</view>
			</scroll-view>
			<u-loadmore :status="status" />
		</block>
		<u-gap height="200" v-if="list.length != 0"></u-gap>
		<u-button class="base-add" type="primary" @click="navTo('accountForm', { type: 0 })">
			<u-icon class="base-add-icon" name="plus" color="#fff" size="28"></u-icon>新增账号
		</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				status: 'loadmore',
				keyword: '',
				popupShow: false,
				statusCurrent: 0,
				statusOptions: [
					{
						label: '全部',
						value: null,
					},
					// {
					// 	label: '测试1',
					// 	value: 0,
					// },
					// {
					// 	label: '测试2',
					// 	value: 1,
					// },
				],
				list: [
					// {
					// 	title: '13888888888',
					// 	title1: '张三',
					// 	content: '浙江院区',
					// 	roleId: 2,
					// 	roleName: '院区管理员'
					// },
					// {
					// 	title: '13888888889',
					// 	title1: '李四',
					// 	content: '浙江院区',
					// 	roleId: 3,
					// 	roleName: '业务员'
					// },
				],
				page: 1,
				limit: 10,
				total: 0
			};
		},
		computed: {
			userInfo() {
				if (Object.keys(this.$store.state.userInfo).length != 0) {
					return this.$store.state.userInfo
				} else if (Object.keys(uni.getStorageSync('userInfo')).length != 0) {
					return uni.getStorageSync('userInfo')
				}
				return {}
			}
		},
		onReachBottom() {
			// 判断数据是否全部加载完毕，true则停止请求接口，修改loadingmore状态
			if (this.list.length >= this.total) {
				this.status = 'nomore'
				return;
			}
			this.status = 'loading';
			this.page += 1;
			this.getListData(true); //调用数据
		},
		methods: {
			getListData(isPush = false) {
				this.$api.user({
					page: this.page,
					limit: this.limit,
					keyWord: this.keyword || null,
					deptId: this.statusVal
				}).then(res => {
					let data = res.data
					this.total = res.count
					if (!isPush) {
						this.list = []
					}
					if (this.total < this.limit) { // 不足10条数据
						this.status = 'nomore'
					}
					for (let i of data) {
						this.list.push(i)
					}
				})
			},
			getDepList(index) {
				this.$api.deptList({
					current: 1,
					size: 999
				}).then(res => {
					let data = res.data
					for (let i of data.records) {
						this.statusOptions.push({
							label: i.deptName,
							value: i.deptId,
							extra: i.sort
						})
					}
					if (index) {
						this.statusCurrent = index;
					}
				})
			},
			navTo(path, params) {
				this.$Router.push({ name: path, params })
			},
			reset() {
				this.status = 'loading';
				this.page = 1
				this.list = []
				this.getListData()
			},
			searchChange(value) {
				this.reset()
			},
			downSelChange(index) {
				this.statusCurrent = index;
				this.statusVal = this.statusOptions[index].value
				this.popupShow = false
				this.reset()
			},
			delAccount(id) {
				uni.showModal({
					content: '确定要删除该帐号吗？',
					success: (res) => {
						if (res.confirm) {
							this.$api.userDel({id}).then(res => {
								this.getListData()
								uni.showToast({
									title: '删除成功！'
								})
							})
						}
					}
				})
			}
		},
		onLoad() {
			const {
				deptId,
				deptIndex
			} = this.$Route.query;
			this.statusVal = deptId;
			this.getListData()
			this.getDepList(deptIndex + 1)
		}
	}
</script>

<style>
	page {
		background-color: #F7F7F7;
	}
	.notic-title{
		font-size: 28rpx;
	}
	.notic-desc{
		margin-top: 24rpx;
		font-size: 24rpx;
		justify-content: space-between;
		align-items: center;
	}
	.list-search {
		padding: 24rpx 28rpx;
		background-color: #fff;
	}
	.downMenu{
		padding: 8rpx 15rpx;
		background: #F7F7F7;
		border-radius: 4rpx;
		margin: 0 30rpx 30rpx 0;
		border: 1rpx solid #F7F7F7;
	}
	
	.silk-ribbon {
		display: flex;
		justify-content: center;
		position: absolute;
		right: 0;
		color: #FFFFFF;
	}
	
	.silk-ribbon:nth-child(even) {
		margin-right: 4%;
	}
	
	.silk-ribbon1 {
		position: absolute;
		top: -18px;
		right: 25px;
	}
	
	.silk-ribbon1:after {
		position: absolute;
		content: "";
		display: block;
		width: 0;
		height: 0;
		border-left: 53px solid transparent;
		border-right: 53px solid transparent;
		border-top: 10px solid #379b9c;
	}
	
	.silk-ribbon1 span {
		position: relative;
		display: inline-block;
		text-align: center;
		background: #379b9c;
		font-size: 14px;
		line-height: 1;
		padding: 12px 8px 10px;
		border-top-right-radius: 8px;
		width: 90px;
	}
	
	.silk-ribbon1 span:before,
	.silk-ribbon1 span:after {
		position: absolute;
		content: "";
		display: block;
	}
	
	.silk-ribbon1 span:before {
		background: #379b9c;
		height: 6px;
		width: 6px;
		left: -6px;
		top: 0;
	}
	
	.silk-ribbon1 span:after {
		background: #337e7f;
		height: 6px;
		width: 8px;
		border-radius: 8px 8px 0 0;
		left: -8px;
		top: 0;
	}

	.silk-ribbon1.green:after{
		border-top-color: #00B96B;
	}
	.silk-ribbon1.green span,
	.silk-ribbon1.green span:before,
	.silk-ribbon1.green span:after{
		background: #00B96B;
	}
	
	.silk-ribbon1.yellow:after{
		border-top-color: #F2BD27;
	}
	.silk-ribbon1.yellow span,
	.silk-ribbon1.yellow span:before,
	.silk-ribbon1.yellow span:after{
		background: #F2BD27;
	}
</style>
